---
import demoPreviewImageUrl from '~demos/utils/demoPreviewImageUrl';
import defaultTeaserImage from '../images/20221122-dark-circles-back-1024x.jpg?url';

export interface Props {
  id: string;
  title: string;
  body: string;
  href: string;
  image?: string;
  tags?: string[];
}

const {id, href, title, body, image, tags} = Astro.props;

const previewImage = image ? demoPreviewImageUrl(image) : defaultTeaserImage;
---

<li class="link-card" data-demo-id={id}>
  <a href={href}>
    <div class="card-image-container">
      <img src={previewImage} class="card-image" />
    </div>
    <h2>
      {title}
    </h2>
    <p>
      {body}
    </p>
    {
      tags && tags.length > 0 && (
        <ul class="tags">
          {tags.map((tag) => (
            <li class="tag">{tag}</li>
          ))}
        </ul>
      )
    }
  </a>
</li>

<script>
  document.querySelectorAll('.link-card').forEach(($el) => {
    const demoId = $el.getAttribute('data-demo-id');
    document.addEventListener('lookbook.showDemos', (event: any) => {
      if (event.detail.showAll || event.detail.filterById?.includes?.(demoId)) {
        $el.classList.remove('hidden');
      } else {
        $el.classList.add('hidden');
      }
    });
  });
</script>

<style>
  .link-card {
    list-style: none;
    display: flex;
    padding: 0;
    background-color: var(--color-background-dark);
    border-radius: 0.5rem;
    padding: 0;
    overflow: hidden;
  }

  .link-card.hidden {
    display: none;
  }

  .link-card > a {
    width: 100%;
    text-decoration: none;
    line-height: 1.4;
    padding: 0 1.3rem 0.7rem;
    border-radius: 0.35rem;
    color: var(--color-primary);
  }
  h2 {
    margin: 1rem 0 0.5rem;
    font-size: 1rem;
    font-weight: bold;
  }
  p {
    margin-top: 0.5rem;
    margin-bottom: 1rem;
    color: var(--color-primary-darker);
    font-size: 0.95rem;
  }
  .link-card:is(:hover, :focus-within) {
    box-shadow: 0 0 1px 1px var(--color-primary-highlight);
  }
  .link-card:is(:hover, :focus-within) h2 {
    color: var(--color-primary-highlight);
  }
  .card-image-container {
    margin: 0 -1.3rem;
    aspect-ratio: 16/10;
    overflow: hidden;
  }
  .card-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: cover;
    aspect-ratio: 16/10;
  }
  .tags {
    margin: -0.1rem -0.3rem 0.5rem;
  }
  .tag {
    display: inline-block;
    font-size: 0.8rem;
    background-color: var(--color-background);
    margin: 0.2rem;
    padding: 0.05rem 0.3em 0.1rem;
    border-radius: 0.4rem;
  }
</style>
